#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqqueue.h"


//创建队列
sqqueue queue_create(){

    sqqueue sq = ( sqqueue )malloc( sizeof( sqnode ) );
    if( sq == NULL )
    {
	printf("malloc failed\n");
	return NULL;
    }

    memset( sq->data, 0, sizeof( sq->data ) );
    sq->front = sq->rear =0;

    return sq;
}


//入队
int enqueue( sqqueue sq, data_t value ){

    if( sq ==NULL ){
    
	printf("sq is NULL\n");
	return -1;
    }

    if( ( sq->rear +1) % N == sq->front )
    {
	printf("sq is full\n");
	return -1;
    }

    sq->data[sq->rear] = value;
    sq->rear = ( sq->rear + 1 ) % N;

    return 1;
}

//出队
data_t dequeue( sqqueue sq ){
    
    if( sq ==NULL )
    {
	printf("sq is NULL\n");
	return -1;
    }

    data_t res = sq->data[sq->front];
    sq->front = ( sq->front + 1 ) % N;
    
    return res;
}

//队列是否为空
int queue_empty( sqqueue sq ){
    
    if( sq ==NULL )
    {
	printf("sq is NULL\n");
	return -1;
    }

    return sq->front == sq->rear ? 1 : 0;
}    

//队列是否满了
int queue_full( sqqueue sq ){
 
    if( sq ==NULL )
    {
	printf("sq is NULL\n");
	return -1;
    }

    return  ( sq->rear + 1) + 1 == sq->front ? 1 : 0;
}    

//清空队列
int queue_clear( sqqueue sq ){
    
   if( sq ==NULL )
   { 
	printf("sq is NULL\n");
	return -1;
    }

    sq->front = sq->rear =0;

    return 1;
}    

//释放队列
sqqueue queue_free( sqqueue sq ){
    if( sq ==NULL )
    {
	printf("sq is NULL\n");
	return NULL;
    }

    free( sq );
    sq = NULL;

    return NULL;
}    
